﻿/**
 * Jean-François Desgagné 2009 Portfolio
 * Copyright (c) 2009 Jean-François Desgagné <jfesgagne@gmail.com>
 * 
 * This work is licensed under a Creative Commons Attribution 3.0 United States License.
 * Some Rights Reserved.
 */
package com.jfdesgagne.jfdesgagne09.shell.view {
    import com.jfdesgagne.jfdesgagne09.shell.ApplicationFacade;
    import com.jfdesgagne.jfdesgagne09.shell.model.LocaleProxy;
	import com.jfdesgagne.jfdesgagne09.shell.model.FrameProxy;
	import com.jfdesgagne.jfdesgagne09.shell.view.component.Background;
	import com.jfdesgagne.jfdesgagne09.shell.view.component.Frame;
    import com.jfdesgagne.jfdesgagne09.shell.view.component.Site;
	import com.jfdesgagne.jfdesgagne09.shell.view.component.Tooltip;
	import com.jfdesgagne.utils.event.StringEvent;
	import com.jfdesgagne.jfdesgagne09.shell.view.event.TooltipEvent;
	import flash.display.DisplayObject;
	import fl.lang.Locale;
    
    import org.puremvc.as3.multicore.interfaces.*;
    import org.puremvc.as3.multicore.patterns.mediator.Mediator;
    
    public class SiteMediator extends Mediator implements IMediator {
        // Cannonical name of the Mediator
        public static const NAME:String = "SiteMediator";
        private var _localeProxy:LocaleProxy;

        public function SiteMediator(viewComponent:Object) {
            super(NAME, viewComponent);
        }

		override public function listNotificationInterests():Array {
            return [ 
					ApplicationFacade.INITIALIZE,
            		ApplicationFacade.SECTION_CHANGED,
					ApplicationFacade.RESIZE
                   ];
        }

        override public function handleNotification(note:INotification):void {
        	switch (note.getName()) {
                case ApplicationFacade.SECTION_CHANGED:
					
				break;
				
				case ApplicationFacade.INITIALIZE:
					this.initalizeComponents();
					this.resize();
				break;
				
				case ApplicationFacade.RESIZE:
					this.resize();
				break;
            }
        }
		
		private function initalizeComponents():void {
			this.site.init();
			this.site.background.init();
			this.site.frame.init();
			
			this.site.frame.logo_mc.subTitle_txt.text = Locale.loadString("IDS_LOGO_SUBTITLE");
			
			
			var mainMenu:Array = FrameProxy.getInstance().mainMenu;
			for (var i:Number = 0; i < mainMenu.length; i++) {
				this.site.frame.createMainMenuItem(mainMenu[i].name, mainMenu[i].label, mainMenu[i].subLabel, mainMenu[i].tooltip, mainMenu[i].lettre, mainMenu[i].email);
			}
			
			var bottomMenu:Array = FrameProxy.getInstance().bottomMenu;
			for (i = 0; i < bottomMenu.length; i++) {
				this.site.frame.createBottomMenuItem(bottomMenu[i].name, bottomMenu[i].label, mainMenu[i].tooltip, bottomMenu[i].lettre, bottomMenu[i].border);
			}
			
			this.site.frame.addEventListener(Frame.MAINMENU_CLICK, this.mainMenuClickHandler);
			this.site.frame.addEventListener(Frame.BOTTOMMENU_CLICK, this.bottomMenuClickHandler);
			this.site.frame.addEventListener(TooltipEvent.SHOW, this._showTooltip);
			this.site.frame.addEventListener(TooltipEvent.HIDE, this._hideTooltip);
		}
		
		private function _showTooltip(event:TooltipEvent):void {
			var note:Object = new Object();
				note.clip 	= event.clip as DisplayObject;
				note.text 	= event.text;
				note.letter = event.letter
			sendNotification(ApplicationFacade.SHOW_TOOLTIP, note);
		}
		
		private function _hideTooltip(event:TooltipEvent):void {
			sendNotification(ApplicationFacade.HIDE_TOOLTIP, event.clip);
		}
		
		private function mainMenuClickHandler(event:StringEvent):void {
			sendNotification(ApplicationFacade.SECTION_CHANGED, event.text);
		}
		
		private function bottomMenuClickHandler(event:StringEvent):void {
			trace("bottomMenuClickHandler");
			//sendNotification(ApplicationFacade.SECTION_CHANGED, event.text);
		}
		
		private function resize():void {
			this.site.background.setMask();
			this.site.background.setTrame();
			this.site.frame.resize();
		}
		
		public static function getInstance():SiteMediator {
			return ApplicationFacade.getInstance(ApplicationFacade.NAME).retrieveMediator(SiteMediator.NAME) as SiteMediator;
		}
                
        public function get site():Site {
            return this.viewComponent as Site;
        }
    }
}